﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;
using hedefgrup.reports.common;

public partial class r_face_login : reportPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        logindiv.Visible = true;
        projectsdiv.Visible = false;

        string u = base.getString("unm");
        string p = base.getString("pwd");
        string prj = base.getString("prj");
        string up = base.getString("up");

        if (!string.IsNullOrEmpty(up))
        {
            byte[] upBytes = System.Convert.FromBase64String(up);
            up = Encoding.UTF8.GetString(upBytes);
            unm.Text = up.Split('|')[0];
            pwd.Text = up.Split('|')[1];
            loginbutton_Click(this, null);
        }

        if (user.Current == null && string.IsNullOrEmpty(u) && string.IsNullOrEmpty(p)) return;

        if (user.Current != null)
        {
            if (!string.IsNullOrEmpty(prj))
            {
                if ((new List<string>(user.Current.getProjects()).Contains(prj)) || user.Current.getIsAdministrator())
                {
                    base.userProjectName = prj;
                    Report_Abstract.Current = null;
                    Response.Redirect("index.aspx");
                }
                else
                {
                    Response.Write("Seçtiğiniz projeye yetkiniz bulunmamaktadır");
                    return;
                }
            }
            else
            {
                projectsdiv.Visible = true;
                logindiv.Visible = false;
            }
        }

        if (!string.IsNullOrEmpty(u) && !string.IsNullOrEmpty(p))
        {
            loginbutton_Click(null, null);
        }
    }

    protected void loginbutton_Click(object sender, EventArgs e)
    {
        user usr = user.getUserByCredentials(unm.Text, pwd.Text);

        if (usr == null)
        {
            Response.Write("alert(\"Kullanıcı adı ve/veya parola hatalı. Lütfen tekrar deneyiniz.\");");
        }
        else
        {
            user.Current = usr;
            logindiv.Visible = false;
            projectsdiv.Visible = true;

            List<string> projects = new List<string>(usr.getIsAdministrator() ? new string[] { Global.PROJECT_QVN, Global.PROJECT_QVN2013, Global.PROJECT_QRA, Global.PROJECT_SATIS_SUREC, Global.PROJECT_SSRD } : usr.getProjects());

            if (projects.Count == 1)
            {
                Response.Redirect("index.aspx");
            }

            if (!projects.Contains(Global.PROJECT_QVN2013))
            {
                FindControl("project_" + Global.PROJECT_QVN2013).Visible = false;
            }

            if (!projects.Contains(Global.PROJECT_SATIS_SUREC))
            {
                FindControl("project_" + Global.PROJECT_SATIS_SUREC).Visible = false;
            }

            if (!projects.Contains(Global.PROJECT_SSRD))
            {
                FindControl("project_" + Global.PROJECT_SSRD).Visible = false;
            }
        }
    }
}